package vashion.azeroth.core.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import vashion.azeroth.contant.MemberErrorEnum;
import vashion.azeroth.core.manager.personal.PersonalMemberManager;
import vashion.azeroth.core.manager.personal.PersonalProfilesManager;
import vashion.azeroth.core.personal.pojo.PersonalProfiles;
import vashion.azeroth.core.result.ApiResult;
import vashion.azeroth.core.service.PersonalProfilesService;

@Service
public class PersonalProfilesServiceImpl<T> implements
		PersonalProfilesService<T> {

	public static final int FOLLOW = 1;
	public static final int  FANS = 2;

	@Autowired
	private PersonalProfilesManager personalProfilesManager;

	@Autowired
	private PersonalMemberManager personalMemberManager;

	/**
	 * 
	 * */

	// @Override
	public ApiResult<T> addProfiles(PersonalProfiles profiles) {
		ApiResult<T> result = new ApiResult<T>();

		profiles.setTimeAdd(new Date(System.currentTimeMillis()));
		profiles.setTimeEdit(new Date(System.currentTimeMillis()));

		if (StringUtils.isBlank(profiles.getMemberName())) {
			profiles.setMemberName(personalMemberManager.get(
					profiles.getMemberId()).getMemberName());
		}
		PersonalProfiles res = personalProfilesManager.save(profiles);
		try {
			Long memberId = res.getMemberId();
			if (null != memberId) {
				result = ApiResult.newSuccessResult();
			}

		} catch (Exception e) {
			return ApiResult.newErrorResult(
					MemberErrorEnum.ADD_PROIFLES_FAILED.getErrorCode(),
					MemberErrorEnum.ADD_PROIFLES_FAILED.getErrorMessage());
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	public ApiResult<PersonalProfiles> getProfiles(PersonalProfiles profiles) {
		ApiResult<T> result = new ApiResult<T>();

		int profileCount = personalProfilesManager.checkProfileCnt(profiles
				.getMemberId());
		if (profileCount == 0) {
			ApiResult<T> addRes = addProfiles(profiles);
			System.out.println(addRes.getData());
			return (ApiResult<PersonalProfiles>) addRes;
		}

		ArrayList<PersonalProfiles> resProfiles = (ArrayList<PersonalProfiles>) personalProfilesManager
				.find(profiles);
		result = (ApiResult<T>) ApiResult.newSuccessResult(resProfiles);
		return (ApiResult<PersonalProfiles>) result;
	}

	@SuppressWarnings("unchecked")
	public ApiResult<T> updProfiles(PersonalProfiles profiles) {
		int profileCount = personalProfilesManager.checkProfileCnt(profiles
				.getMemberId());
		if (profileCount == 0) {
			profiles.setTimeAdd(new Date(System.currentTimeMillis()));
			profiles.setTimeEdit(new Date(System.currentTimeMillis()));
			personalProfilesManager.save(profiles);
		}

		try {
			int count = personalProfilesManager.update(profiles);
			if(count > 0) {
				return (ApiResult<T>) ApiResult.newSuccessResult(profiles);
			} else {
				return ApiResult.newErrorResult(
						MemberErrorEnum.GET_PROIFLES_FAILED.getErrorCode(),
						MemberErrorEnum.GET_PROIFLES_FAILED.getErrorMessage());
			}
		} catch (Exception e) {
			return ApiResult.newErrorResult(
					MemberErrorEnum.GET_PROIFLES_FAILED.getErrorCode(),
					MemberErrorEnum.GET_PROIFLES_FAILED.getErrorMessage());
		}
	}

	public boolean updateFansCount(Long memberId, int type, String action) {

		boolean flag = false;
		PersonalProfiles profiles = new PersonalProfiles();
		profiles.setMemberId(memberId);
		ArrayList<PersonalProfiles> resProfiles = (ArrayList<PersonalProfiles>) personalProfilesManager.find(profiles);
		int newFansCnt = 0;
		int newFollowCnt = 0;
		if (null == resProfiles || resProfiles.size() == 0) {
			newFansCnt = 1;
			newFollowCnt = 1;
		}else {
			newFansCnt = updateInfo(resProfiles.get(0).getFansCount(), action);
			newFollowCnt = updateInfo(resProfiles.get(0).getFollowCount(), action);
		}
			switch (type) {
			case FOLLOW:
				PersonalProfiles followProfiles = new PersonalProfiles();
				followProfiles.setFollowCount(newFollowCnt);
				followProfiles.setMemberId(memberId);
				followProfiles.setTimeAdd(new Date(System.currentTimeMillis()));
				followProfiles.setTimeAdd(new Date(System.currentTimeMillis()));
				updProfiles(followProfiles);
				flag=true;
				break;

			case FANS:
				PersonalProfiles fansProfiles = new PersonalProfiles();
				fansProfiles.setFansCount(newFansCnt);
				fansProfiles.setMemberId(memberId);
				fansProfiles.setTimeAdd(new Date(System.currentTimeMillis()));
				fansProfiles
						.setTimeEdit(new Date(System.currentTimeMillis()));
				updProfiles(fansProfiles);
				flag=true;
				break;
			}

		return flag;
	}

	public int updateInfo(int infoCount, String action) {

		if (action.equals("up")) {
			infoCount += 1;
		} else if (action.equals("down")) {
			infoCount = infoCount - 1;
		}
		
		// 防止count值更新为负数
		if (infoCount <= 0) {
			return 0;
		}

		return infoCount;
	}

}
